<!--
  * 巡检线路
  *
-->
<template>
  <a-modal
      :title="form.id ? '编辑' : '添加'"
      width="600px"
      :open="visibleFlag"
      @cancel="onClose"
      :maskClosable="false"
      :destroyOnClose="true"
  >
    <a-form ref="formRef" :model="form" :rules="rules" :label-col="{ span: 8 }" >

                    <a-form-item label="线路名称"  name="name">
                      <a-input style="width: 300px" v-model:value="form.name" placeholder="线路名称" />
                    </a-form-item>




        <a-form-item label="组织机构"  name="deptId">
<!--
          <DictSelect style="width: 300px" v-model:value="form.deptId" keyCode="" placeholder="组织机构"/>
-->
          <DepartmentTreeSelect style="width: 300px"   ref="departmentTreeSelect" width="100%" :init="false" v-model:value="form.deptId" />

        </a-form-item>




        <a-form-item label="备注"  name="remark">
          <a-textarea style="width: 300px"  v-model:value="form.remark" placeholder="备注" />
        </a-form-item>


    </a-form>

    <template #footer>
      <a-space>
        <a-button @click="onClose">取消</a-button>
        <a-button type="primary" @click="onSubmit">保存</a-button>
      </a-space>
    </template>
  </a-modal>
</template>
<script setup>
  import { reactive, ref, nextTick } from 'vue';
  import _ from 'lodash';
  import { message } from 'ant-design-vue';
  import { SmartLoading } from '/src/components/framework/smart-loading';
  import { gInspectionLinesApi } from '/@/api/business/xjgl/g-inspection-lines-api';
  import { smartSentry } from '/src/lib/smart-sentry';
          import DictSelect from '/src/components/support/dict-select/index.vue';
  import DepartmentTreeSelect from "/@/components/system/department-tree-select/index.vue";

  const departmentTreeSelect = ref();
  // ------------------------ 事件 ------------------------

  const emits = defineEmits(['reloadList']);

  // ------------------------ 显示与隐藏 ------------------------
  // 是否显示
  const visibleFlag = ref(false);

  function show(rowData) {
    Object.assign(form, formDefault);
    if (rowData && !_.isEmpty(rowData)) {
      Object.assign(form, rowData);
    }
    visibleFlag.value = true;
    nextTick(() => {
      formRef.value.clearValidate();
    });
  }

  function onClose() {
    Object.assign(form, formDefault);
    visibleFlag.value = false;
  }

  // ------------------------ 表单 ------------------------

  // 组件ref
  const formRef = ref();

  const formDefault = {
          id: undefined,
              name: undefined, //线路名称
              deptId: undefined, //组织机构
              remark: undefined, //备注
  };

  let form = reactive({ ...formDefault });

  const rules = {
                  name: [{ required: true, message: '线路名称 必填' }],
                  deptId: [{ required: true, message: '组织机构 必填' }],
  };

  // 点击确定，验证表单
  async function onSubmit() {
    try {
      await formRef.value.validateFields();
      save();
    } catch (err) {
      message.error('参数验证错误，请仔细填写表单数据!');
    }
  }

  // 新建、编辑API
  async function save() {
    SmartLoading.show();
    try {
      if (form.id) {
        await gInspectionLinesApi.update(form);
      } else {
        await gInspectionLinesApi.add(form);
      }
      message.success('操作成功');
      emits('reloadList');
      onClose();
    } catch (err) {
      smartSentry.captureError(err);
    } finally {
      SmartLoading.hide();
    }
  }

  defineExpose({
    show,
  });
</script>
